var xmlhttp = false;
var todo_bo_table = '';
var todo_wr_id = '';
var todo_wr_subject = '';

/*
// 오류가 나서 수정
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.overrideMimeType('text/xml');
} else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}*/

// 그누보드 기본 Ajax.js파일을 참고 2008-07-07
var xmlhttp = null;
try {
	xmlhttp = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		xmlhttp = new ActiveXObject("Msxml12.XMLHTTP");
	} catch (othermicrosoft) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			xmlhttp = null;
		}
	}
}
if (xmlhttp == null)
	alert("Error creating request object!");



var active=false; // 추가중 일때에는 비활성화된다.

/* --------------------------------------------------------------------
	리스트 추가
*/
function insertTODO(bo_table) {
	if(active==true) return;
    var subject = document.getElementById("todo").value;
    subject = trim(subject);

    todo_bo_table = bo_table;
    todo_wr_subject = subject;

    if(subject != '') {
        var url = g4_path + "/class/Ajax/TodoList_insert.ajax.php?wr_subject=" + encodeURIComponent(subject) + "&bo_table=" + todo_bo_table;

        xmlhttp.open('GET', url, true);
        xmlhttp.onreadystatechange = handleInsert;
        xmlhttp.send(null);
		active = true;
    }

    document.getElementById("todo").value = "";
}


function handleInsert() {
    var obj = document.getElementById("todoList");
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

		if(xmlhttp.responseText == 'empty') {
			obj.innerHTML = "<ul><li>제목을 입력 해야 합니다.</li></ul>"+obj.innerHTML;
		} else if(xmlhttp.responseText == 'error') {
            obj.innerHTML = "<ul><li>추가할 권한이 없습니다.</li></ul>"+obj.innerHTML;
		} else {
            todo_wr_id = xmlhttp.responseText;

			var html="";
			html += "<ul>";
			html += "<li id='"+todo_bo_table+todo_wr_id+"'>";
			html += "<a class=button href=\"javascript:completeTODO('"+todo_bo_table+"', "+ todo_wr_id+", '"+todo_wr_subject+"' );\" >";
			html += "<img src='"+g4_path + "/class/img/TodoList/complete.gif'>";
			html += "</a>";
			html += "<a  href='"+ g4_path + "/bbs/board.php?bo_table="+todo_bo_table+"&wr_id="+todo_wr_id+"'>"+todo_wr_subject+"</a>";
			html += "</li>";
			html += "</ul>";

			obj.innerHTML = html + obj.innerHTML;

        }

		active = false;
    }
}

function trim(s) {
	s += ''; // 숫자라도 문자열로 변환
	return s.replace(/^\s*|\s*$/g, '');
}

/* -------------------------------------------------------------------- */


/* --------------------------------------------------------------------
	완료 설정
*/

function completeTODO(bo_table, wr_id, wr_subject) {
	if(active==true) return;
    todo_bo_table = bo_table;
    todo_wr_id = wr_id;
    todo_wr_subject = wr_subject;
    var url = g4_path + "/class/Ajax/TodoList_complete.ajax.php?bo_table=" + bo_table + "&wr_id=" + wr_id;
    xmlhttp.open('GET', url, true);
    xmlhttp.onreadystatechange = handleComplete;
    xmlhttp.send(null);
	active = true;
}

function handleComplete() {
    var obj = document.getElementById(todo_bo_table+todo_wr_id);
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {        
        if(xmlhttp.responseText == 'error') {
            obj.innerHTML = "수정할 권한이 없습니다.";
        } else if(xmlhttp.responseText == 'success') {
            obj.innerHTML = "<a class=button href=\"javascript:unlistTODO('"+todo_bo_table+"', "+todo_wr_id+", '"+todo_wr_subject+"' );\" ><img src='"+g4_path+"/class/img/TodoList/complete.gif'></a><a class=complete href=\""+g4_path + "/bbs/board.php?bo_table="+todo_bo_table+"&wr_id="+todo_wr_id+"\">"+todo_wr_subject+"</a>";
        } else if(xmlhttp.responseText == 'updateList') {
            var todolist = document.getElementById("todoList");
            obj.innerHTML= "";
        }
		active = false;
    } else {
        obj.innerHTML = "서버 통신 오류";
    }
}
/* -------------------------------------------------------------------- */




/* --------------------------------------------------------------------
	리스트에서 제거 설정
*/
function unlistTODO(bo_table, wr_id, wr_subject) {
	if(active==true) return;
    todo_bo_table = bo_table;
    todo_wr_id = wr_id;
    todo_wr_subject = wr_subject;
    var url = g4_path + "/class/Ajax/TodoList_unlist.ajax.php?bo_table=" + bo_table + "&wr_id=" + wr_id;
    xmlhttp.open('GET', url, true);
    xmlhttp.onreadystatechange = handleUnlist;
    xmlhttp.send(null);
	active = true;
}



function handleUnlist() {
    var obj = document.getElementById(todo_bo_table+todo_wr_id);
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {        
        if(xmlhttp.responseText == 'error') {
            obj.innerHTML = "수정할 권한이 없습니다.";
        } else if(xmlhttp.responseText == 'success') {
            obj.style.display = "none";
        } else if(xmlhttp.responseText == 'updateList') {
            var todolist = document.getElementById("todoList");
            obj.innerHTML= "";
        }
		active = false;
    } else {
        obj.innerHTML = "서버 통신 오류";
    }
}

/* -------------------------------------------------------------------- */







